<?php
$docRoot = $_SERVER ['DOCUMENT_ROOT'] . '/' . strtok ( $_SERVER ['PHP_SELF'], '/' );
include_once ($docRoot . "/application/service/feed/parser/FeedParser.php");
include_once ($docRoot . "/application/service/feed/util/HtmlUtil.php");
?>
<?php

class ScoresParserImpl implements FeedParser {
	private $feedUrl = "404";
	
	public function setFeedUrl($url) {
		$this->feedUrl = $url;
	}
	
	/**
	 * logic to get feed in html    format for score type
	 * (non-PHPdoc)
	 * @see FeedParser::getFeedHtml()
	 */
	public function getFeedHtml() {
		$url = $this->feedUrl;
		$outputHTML = HtmlUtil::createHtmlDiv ( "error", "Invalid Request" );
		
		if ($url != "404") {
			$outputHTML = "";
			$xmlObject = simplexml_load_file ( $url );
			$outputHTML = "<div id='score-catagory-wrapper'>";
			
			foreach ( $xmlObject->children () as $catagory ) {
				$catagoryAttr = $catagory->attributes (); // returns an array
				$outputHTML .= "<div class='catagory'>";
				//push catagory attribute
				$outputHTML .= "<div class='info w100 h20'>";
				$outputHTML .= HtmlUtil::createHtmlDiv ( "name left w60", $catagoryAttr ["name"] );
				$outputHTML .= HtmlUtil::createHtmlDiv ( "country right w40", $catagoryAttr ["country"] );
				$outputHTML .= "</div>";
				
				$outputHTML .= HtmlUtil::createHtmlDiv ( "clear", "" );
				
				//get all match node
				foreach ( $catagory->children () as $match ) {
					$matchAttr = $match->attributes ();
					
					$outputHTML .= "<div class='match'>";
					$outputHTML .= "<div class='info'>";
					$outputHTML .= "<div class='date'>" . $matchAttr ['date'] . "</div>";
					$outputHTML .= "<div class='time'>" . $matchAttr ['time'] . "</div>";
					$outputHTML .= "<div class='status'>" . $matchAttr ['status'] . "</div>";
					$outputHTML .= "</div>";
					
					$matchInfo = $match->children ();
					
					$localteamAttr = $matchInfo [0]->attributes ();
					$outputHTML .= "<div class='localteamName'><a href='#'>" . $localteamAttr ['name'] . "</a></div>";
					
					$awayteamAttr = $matchInfo [1]->attributes ();
					$outputHTML .= "<div class='awayteamName'><a href='#'>" . $awayteamAttr ['name'] . "</a></div>";
					
					//check of events if totalscore="" donot display events
					if ($localteamAttr ['totalscore'] != "") {
						foreach ( $matchInfo [2]->children () as $events ) {
							$eventAttr = $events->attributes ();
							if ($eventAttr ['score'] != "") {
								$outputHTML .= "<div class='score'>" . $events->getName () . " :: " . $eventAttr ['score'] . "</div>";
							}
							foreach ( $events->children () as $scoreDetails ) {
								$scoreDeatilsAttr = $scoreDetails->attributes ();
								$outputHTML .= "<div class='score-details'> Player :" . $scoreDeatilsAttr ['player'] . "</div>";
							}
						
						}
					}
					
					//match div ends
					$outputHTML .= "</div>";
				}
				
				//Catagory Div ends ehre
				$outputHTML .= '</div>';
			}
			//Wrapper Div ends here
			$outputHTML .= '</div>';
		}
		return $outputHTML;
	}
}
?> 